<?php

namespace App\Http\Controllers\Manage;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class AuthController extends Controller
{
    public function index()
    {
        return view('manage.login');
    }

    public function submit(Request $request)
    {
        $username = $request->input('username');
        $password = $request->input('password');
        $user = User::query()->where('name', $username)->first();
        if (!$user) {
            return $this->messageLogin('用户不存在', $username);
        }
        if (!Hash::check($password, $user->password)) {
            return $this->messageLogin('密码不正确', $username);
        }
//        session(['username' => $username]);
        Auth::loginUsingId($user->id);
        return redirect(route('manage.index'));
    }

    public function logout(Request $request)
    {
        Auth::logout();
        return redirect(route('manage.login_index'));
    }

    function messageLogin($info, $username) {
        return view('login', [
            'result' => "layer.msg('" . $info  . "', {icon: 0}, function(){
        // layer.msg('提示框关闭后的回调');
      });",
            'username' => $username,
        ]);
    }


}
